package cn.jetpiece.cloud.system.dp.app.modular.biz.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.jetpiece.cloud.model.response.Result;
import cn.jetpiece.cloud.system.dp.api.biz.model.params.ProjectAddParam;
import cn.jetpiece.cloud.system.dp.app.enums.DataStatusEnum;
import cn.jetpiece.cloud.system.dp.app.modular.biz.entity.DpTProject;
import cn.jetpiece.cloud.system.dp.app.modular.biz.entity.LngLat;
import cn.jetpiece.cloud.system.dp.app.modular.biz.mapper.DpTProjectMapper;
import cn.jetpiece.cloud.system.dp.app.modular.biz.service.IDpTProjectService;
import cn.jetpiece.cloud.system.dp.app.utils.TianApiUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * dp_t_project
 *
 * @author jetpiece
 * @since 2023-05-15
 */
@Service
@Slf4j
public class DpTProjectServiceImpl extends ServiceImpl<DpTProjectMapper, DpTProject> implements IDpTProjectService {

    @Override
    public Result<String> projectInfo(ProjectAddParam param) {
        //封装对象
        DpTProject project = this.buildProject(param);
        DpTProject already = this.findBySourceId(project.getSourceId(), 2);
        if (ObjectUtil.isNull(already)) {
            this.save(project);
        } else {
            log.info("项目已经存在，sourceId:" + param.getProjectId());
        }
        return Result.OK();
    }

    @Override
    public DpTProject findBySourceId(Integer sourceId, int dataSource) {
        LambdaQueryWrapper<DpTProject> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DpTProject::getSourceId, sourceId);
        queryWrapper.eq(DpTProject::getDataSource, dataSource);
        queryWrapper.eq(DpTProject::getDataStatus, DataStatusEnum.NORMAL.getValue());
        List<DpTProject> projectList = this.list(queryWrapper);
        if (CollUtil.isNotEmpty(projectList)) {
            return projectList.get(0);
        }
        return null;
    }

    @Override
    public DpTProject getByLicenceNum(String builderLicenseNum) {
        LambdaQueryWrapper<DpTProject> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DpTProject::getBuilderLicenseNum, builderLicenseNum);
        queryWrapper.eq(DpTProject::getDataStatus, DataStatusEnum.NORMAL.getValue());
        List<DpTProject> projectList = this.list(queryWrapper);
        if (CollUtil.isNotEmpty(projectList)) {
            return projectList.get(0);
        }
        return null;
    }


    private DpTProject buildProject(ProjectAddParam param) {
        DpTProject p = new DpTProject();
        //数据状态取反
        p.setDataStatus(this.negate(param.getIsDel()));
        p.setProjectName(param.getProjectName2());
        p.setBuilderLicenseNum(param.getProjectChartReviewNum());
        p.setProvinceCode(param.getProvinceNum());
        p.setCityCode(param.getCityNum());
        p.setCountyCode(param.getCountryNum());
        p.setProjectAddress(param.getProjectAddress());
        p.setLat(param.getLatitude());
        p.setLng(param.getLongitude());
        p.setProjectUuid(param.getProjectUuid());
        p.setLinkMan(param.getPlaceChargeUser());
        p.setLinkPhone(param.getPlaceChargeUserPhone());
        p.setDescription(param.getMemo());
        p.setCreateTime(new Date());
        p.setDataSource(2);
        p.setSourceId(param.getProjectId());
        //增加获取地址坐标位置
        addTianLocation(p);
        return p;
    }


    private Integer negate(Integer isDel) {
        if (isDel == null) {
            return null;
        }
        if (DataStatusEnum.NORMAL.getValue().equals(isDel)) {
            return DataStatusEnum.DELETE.getValue();
        } else if (DataStatusEnum.DELETE.getValue().equals(isDel)) {
            return DataStatusEnum.NORMAL.getValue();
        } else {
            return isDel;
        }
    }


    /**
     * @Description: 增加天地图地址解析
     * @Author: zdingxin
     * @Date: 2020/12/15 17:51
     */
    private void addTianLocation(DpTProject p) {
        if (StringUtils.isEmpty(p.getLng()) && StringUtils.isEmpty(p.getLng())) {
            try {
                if (!StringUtils.isEmpty(p.getProjectAddress())) {
                    LngLat lngLat = TianApiUtil.getLocation(p.getProjectAddress());
                    if (lngLat != null) {
                        p.setLng(lngLat.getLon());
                        p.setLat(lngLat.getLat());
                        p.setRemark("tianditu坐标");
                    }
                }
            } catch (Throwable e) {
                log.info("获取地图信息出现异常,{}", e.getMessage());
            }
        }
    }
}
